package com.itheima.service;

import com.itheima.admin.AdminPage;
import com.itheima.common.SnowFlake;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.admin.setmealpojo.SetmealDishes;
import com.itheima.admin.setmealpojo.dto.SetmealDto;
import com.itheima.admin.setmealpojo.vo.SetmealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wuzhiyong
 */
@Service
public class SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SnowFlake snowFlake;

    /**
     * 添加套餐
     *
     * @param setmealDto setmealDto
     * @return 条数>0
     */
    public boolean addSetmeal(SetmealDto setmealDto) {
        //通过雪花算法获取id
        Long id = snowFlake.nextId();
        //设置id
        setmealDto.setId(String.valueOf(id));
        //获取套餐中菜品
        List<SetmealDishes> setmealDishes = setmealDto.getSetmealDishes();
        if (setmealDishes==null){
            return false;
        }
        for (SetmealDishes dishes : setmealDishes) {
            if (dishes==null){
                continue;
            }
            dishes.setSetmealId(String.valueOf(id));
            setmealMapper.insertSetmealDishes(dishes);
        }
        return setmealMapper.insertSetmeal(setmealDto) > 0;
    }

    /**
     * 分页查询
     * @param name name
     * @param page page
     * @param pageSize pageSize
     * @return adminPage
     */
    public AdminPage<SetmealVo> getpageSetmeal(String name, Integer page, Integer pageSize) {
        //获取索引startIndex
        int startIndex = (page - 1) * pageSize;
        //获取总页数
        int total = setmealMapper.selectAllSetmeal();
        //获取分页后内容
        List<SetmealVo> setmealVos = setmealMapper.selectPageSetmeal(name, startIndex, pageSize);
        //组装响应体并返回
        AdminPage<SetmealVo> adminPage = new AdminPage<>();
        adminPage.setSize(pageSize);
        adminPage.setCurrent(page);
        adminPage.setTotal(total);
        adminPage.setRecords(setmealVos);
        return adminPage;
    }

    /**
     * 删除套餐
     * @param ids ids
     * @return 条数>0
     */
    public boolean removeByIdsSetmeal(String[] ids) {
        if (ids==null){
            return false;
        }
        for (String id : ids) {
            if (id==null){
                continue;
            }
            //查询套餐状态是否为在售
            Integer integer = setmealMapper.selectSetmealStatus(id);
            if (integer == 1) {
                return false;
            }
            setmealMapper.deleteByIdsSetmealDishes(id);
        }
        return setmealMapper.deleteByIdsSetmeal(ids) > 0;
    }

    /**
     * 根据id查询
     * @param id id
     * @return SetmealDto
     */
    public SetmealDto getBySetmealId(String id) {
        return setmealMapper.selectBySetmealId(id);
    }

    /**
     * 修改套餐
     * @param setmealDto setmealDto
     * @return 条数>0
     */
    public boolean updateSetmeal(SetmealDto setmealDto){
        //获取套餐id
        String id = setmealDto.getId();
        //先删除套餐中菜品
        setmealMapper.deleteByIdsSetmealDishes(id);
        //获取前端传来套餐中菜品信息
        List<SetmealDishes> setmealDishes = setmealDto.getSetmealDishes();
        if (setmealDishes==null){
            return false;
        }
        for (SetmealDishes setmealDish : setmealDishes) {
            if (setmealDish==null){
                continue;
            }
            //设置套餐id并重新添加套餐中菜品信息
            setmealDish.setSetmealId(id);
            setmealMapper.insertSetmealDishes(setmealDish);
        }
        return setmealMapper.updateSetmeal(setmealDto)>0;
    }

    /**
     * 批量启停用
     * @param status status
     * @param ids ids
     * @return 条数>0
     */
    public boolean modifyBystatus(Integer status, String[]ids ){
        return setmealMapper.updateByStatus(status, ids)>0;
    }
}
